<template>
  <div class="header">
    <van-cell-group inset class="styled-cell-group">
      <van-field label="账户" :model-value="addr" readonly class="styled-field" />
      <van-field label="余额" :model-value="balance" readonly class="styled-field" />
    </van-cell-group>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import useWeb3 from '../util.js';

const { web3 } = useWeb3();

const addr = ref('');
const balance = ref(0);

const getAccount = async () => {
  const accounts = await web3.eth.getAccounts();
  addr.value = accounts[0];
  web3.eth.getBalance(accounts[0]).then((ret) => {
    balance.value = web3.utils.fromWei(ret, "ether") + " eth";
  });
}

export default {
  name: 'ProfileView',
  setup() {
    onMounted(async () => {
      console.log(`The component is now mounted.`);
      await getAccount();
    })
    return {
      addr,
      balance,
    }
  }
}
</script>

<style scoped>
.header {
  margin-top: 40px;
}

.styled-cell-group {
  background-image: url('../assets/lan.jpg'); 
  border-radius: 10px;
  padding: 20px; 
}

.styled-field {
  margin-bottom: 15px;
  background-color: transparent; 
  border: 1px solid #e9ecef; 
  border-radius: 8px; 
  padding: 12px 15px;
  font-size: 16px; 
  font-weight: 400; 
  color: #333; 
  text-align: left;
}

.van-field__label {
  font-size: 14px; 
  font-weight: 400;
  color: #666;
}
</style>